<?php

require_once 'Customers.php';

class Orders {

    private $_ID;
    private $_createdDate;
    private $_description;
    private $_customerID;
    private $_status;
    private $_customer;

    public function getStatus() {
        return $this->_status;
    }

    public function setStatus($status) {
        $this->_status = $status;
    }

    public function getID() {
        return $this->_ID;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function getCreatedDate() {
        return $this->_createdDate;
    }

    public function setCreatedDate($CreatedDate) {
        $this->_createdDate = $CreatedDate;
    }

    public function getDescription() {
        return $this->_description;
    }

    public function setDescription($Description) {
        $this->_description = $Description;
    }

    public function getCustomerID() {
        return $this->_customerID;
    }

    public function setCustomerID($CustomerID) {
        $this->_customerID = $CustomerID;
    }

    public function getCustomer() {
        return $this->_customer;
    }

    public function setCustomer($customer) {
        $this->_customer = $customer;
    }

    public function add($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `orders`
                                (
                                 `O_CreatedDate`,
                                 `O_Description`,
                                 `O_Cus_ID`)
                    VALUES (
                            :O_CreatedDate,
                            :O_Description,
                            :O_Cus_ID);";
            $parameters = array(
                ':O_CreatedDate', ':O_Description',
                ':O_Cus_ID',
            );
            $values = array(
                $this->getCreatedDate(), $this->getDescription(),
                $this->getCustomerID()
            );
            $types = array(
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_INT
            );
            return $db->executeNonQuery($sql, $parameters, $values, $types, true);
        }
    }

    public function updateStatus($db = null) {
        if ($db != null) {
            $sql = "UPDATE `orders`
                        SET `O_Status` = :O_Status
                        WHERE `O_ID` = :O_ID;";
            $parameters = array(':O_Status', ':O_ID');
            $values = array($this->getStatus(), $this->getID());
            $types = array(PDO::PARAM_INT,PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types, true);
        }
    }
    
    public function delete($db = null) {
        if ($db != null) {
            $sql = "DELETE
                    FROM `orders`
                    WHERE `O_ID` = :O_ID;";
            $parameters = array(':O_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types, true);
        }
    }    

    public function getList($db = null) {
        if ($db != null) {
            $sql = "SELECT
                `O_ID`,
                `O_CreatedDate`,
                `O_Description`,
                `O_Cus_ID`,
                `O_Status`
              FROM `orders`
              ORDER BY O_CreatedDate DESC;";

            $parameters = null;
            $values = null;
            $types = null;
            $result = $db->executeQuery($sql, $parameters, $values, $types, true);

            if ($result != null) {

                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {

                    $order = new Orders();
                    $order->setID($row->O_ID);
                    $order->setCreatedDate($row->O_CreatedDate);
                    $order->setDescription($row->O_Description);
                    $order->setCustomerID($row->O_Cus_ID);
                    $order->setStatus($row->O_Status);


                    // customer
                    $customer = new Customers();
                    $customer->setID($order->getCustomerID());
                    $customer = $customer->getByID($db);
                    $order->setCustomer($customer);

                    $rows[] = $order;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

    public function getByID($db = null) {
        if ($db != null) {
            $sql = "SELECT
                `O_ID`,
                `O_CreatedDate`,
                `O_Description`,
                `O_Cus_ID`,
                `O_Status`
              FROM `orders`
             WHERE O_ID = :O_ID";

            $parameters = array(':O_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            if ($result != null) {

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                $row = $result->fetch();

                $this->setID($row->O_ID);
                $this->setCreatedDate($row->O_CreatedDate);
                $this->setDescription($row->O_Description);
                $this->setCustomerID($row->O_Cus_ID);
                $this->setStatus($row->O_Status);

                // customer
                $customer = new Customers();
                $customer->setID($this->getCustomerID());
                $customer = $customer->getByID($db);
                $this->setCustomer($customer);

                return $this;
            } else {
                return null;
            }
        }
    }

}

?>
